package com.sqx.modules.health.controller;/*
package com.sqx.modules.health.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.auth0.jwt.JWT;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.JsonObject;
import com.sqx.modules.health.common.Constants;
import com.sqx.modules.health.common.Result;
import com.sqx.modules.health.dto.ServiceOrderDTO;
import com.sqx.modules.health.entity.*;
import com.sqx.modules.health.service.*;
import com.sqx.modules.health.utils.HttpUtils;
import com.sqx.modules.health.utils.ObjectUtils;
import com.sqx.modules.health.utils.TaiyiUtil;
import com.sqx.modules.health.utils.WxUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Api(value = "app端支付相关接口	", tags = "app端支付相关接口", description = "")
@RestController
@RequestMapping("/health/app")
public class AppPayController {

    @Autowired
    private IUserService userService;
    @Autowired
    private IAppOrderService appOrderService;

    @Autowired
    private IAppProductService appProductService;

    @ApiOperation(value = "设备购买和租赁", notes = "设备购买和租赁")
    @PostMapping("/payOrder")
    public Result payOrder(@RequestParam Map<String, String> params){

        String token = params.get("token");
        String productid = params.get("productid");
        String type = params.get("type");
        String useday = params.get("useday");

        if (StrUtil.isBlank(token) || StrUtil.isBlank(productid) || StrUtil.isBlank(type) || StrUtil.isBlank(useday)) {
            return Result.error(Constants.CODE_400, "参数错误");
        }
        //String uid = JWT.decode(token).getAudience().get(0);
        String uid = jwtUtils.getUserIdByToken(token);

        User user = userService.getById(uid);
        AppProduct product = appProductService.getById(productid);
        AppOrder saveorder = new AppOrder();
        LocalDateTime createtime = LocalDateTime.now();
        //订单号
        if(Integer.valueOf(type) == 1){//buy
            saveorder.setOrderno(TaiyiUtil.getOrderNo("B"));
            saveorder.setUseday(0);
            saveorder.setCost(product.getOtprice());

        } else if(Integer.valueOf(type) == 2){//hire
            saveorder.setOrderno(TaiyiUtil.getOrderNo("H"));
            saveorder.setUseday(Integer.valueOf(useday));
            saveorder.setCost(product.getRent().multiply(BigDecimal.valueOf(Long.parseLong(useday))).add(product.getOtprice()));

        }
        saveorder.setUid(Integer.valueOf(uid));
        saveorder.setPhone(user.getPhone());
        saveorder.setUname(user.getNickName());
        saveorder.setProductid(productid);
        saveorder.setName(product.getName());//商品名字
        saveorder.setType(type);//1-购买，2-租赁
//        saveorder.setUseday(Integer.valueOf(useday));
        saveorder.setHid(user.getHid());
        saveorder.setStatus(0);//待支付
        saveorder.setEnable("1");//有效
        saveorder.setIsdelete("0");//未删除
        saveorder.setPosition("2");//院外 -agentid
        saveorder.setAddress(user.getAddress());
//        saveorder.setCost(product.getRent().add(product.getOtprice()));//暂时只要一个 =押金+租金

        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String timeStr = df.format(createtime.atZone(ZoneId.systemDefault()));

        saveorder.setCreatetime(timeStr);

        //saveorder.setPaytime(LocalDateTime.now());//暂时不用写，支付成功才写入
//        AppOrderinfo saveorderInfo = new AppOrderinfo();
//        //订单号
//        saveorderInfo.setOrderno(saveorder.getOrderno());
//        saveorderInfo.setProductid(productid);
//        saveorderInfo.setName(product.getName());//商品名字
//        saveorderInfo.setPrice(product.getPrice());//1-购买，2-租赁
//        saveorderInfo.setRent(product.getRent());
////        saveorderInfo.setDiscount(product.getDeposit());
//        saveorderInfo.setDeposit(BigDecimal.valueOf(0));
//        saveorderInfo.setStatus(0);//待支付
//        saveorderInfo.setCreatetime(createtime);//
//        appOrderinfoService.save(saveorderInfo);

        if (appOrderService.save(saveorder)){
            return Result.success(saveorder);
        }else {
            return Result.error(Constants.CODE_600,"订单添加失败");
        }
    }


    @ApiOperation(value = "设备支付成功", notes = "设备支付成功")
    @PostMapping("/paySuccess")
    public Result paySuccess(@RequestParam Map<String, String> params){

        String token = params.get("token");
        String orderno = params.get("orderno");

        if (StrUtil.isBlank(token) || StrUtil.isBlank(orderno)) {
            return Result.error(Constants.CODE_400, "参数错误");
        }
        //String uid = JWT.decode(token).getAudience().get(0);
        String uid = jwtUtils.getUserIdByToken(token);

        QueryWrapper<AppOrder> wrapper = new QueryWrapper<AppOrder>()
                .eq(ObjectUtil.isNotEmpty(orderno), "orderno", orderno)
                .eq(ObjectUtil.isNotEmpty(uid), "uid", uid);

        AppOrder order =  appOrderService.getOne(wrapper);
        order.setPaytime(LocalDateTime.now());
        order.setStatus(1);

        if (appOrderService.saveOrUpdate(order)){
            return Result.success("支付成功");
        }else {
            return Result.error(Constants.CODE_600,"支付失败");
        }
    }

    @GetMapping("/appWeiXinPay")
    public Result appweiXinPay(HttpServletRequest request) throws Exception {
        // 获取用户的IP地址  下单必备参数
        String ipAddress = HttpUtils.getIpAddress(request);

        // 获取服务的id
        String serviceId = request.getParameter("id");
        String orderNo = request.getParameter("orderNo");
        if (StringUtils.isEmpty(orderNo)) {
            return Result.error(Constants.CODE_500, "所选订单不存在");
        }

        // 根据服务的id 获取对应的订单
        AppOrder order = appOrderService.getById(orderNo);
        if (order == null) {
            return Result.error(Constants.CODE_500, "所选订单不存在");
        }

        // 获取当前时间戳
        long millis = System.currentTimeMillis();
        Random random = new Random();

        // 测试付一分钱
//        BigDecimal amount = BigDecimal.valueOf(0.01);

        ServiceOrderDTO prepaidOrder = new ServiceOrderDTO();
        // 服务id
        prepaidOrder.setServiceId(Integer.valueOf(serviceId));
        // 用户的openid
        prepaidOrder.setOpenId(request.getParameter("openid"));
        prepaidOrder.setUserIp(ipAddress);  // 这个是微信需要的参数 userIp
        prepaidOrder.setAmount(order.getCost()); // 这个是微信需要的参数 total_fee
        prepaidOrder.setOrderNumber(orderNo); // 这个是微信需要的参数   out_trade_no
        prepaidOrder.setBody(order.getName());
        // 添加预付订单
        // 组装统一下单需要的数据map
        Map<String, String> stringStringMap = prepaidOrder.toWxPayParameters();
        // 调取统一支付
        Map<String, String> payParameters =  WxUtil.appSendOrder(stringStringMap);//unifiedorder
        return Result.success(payParameters);// 返回预付订单
    }

}

*/
